
#include <gtk-3.0/gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include "audio_device_dialog_widget.h"
#include "audio_dev_box_widget.h"
#include "tux_gsettings.h"

typedef struct audio_device_dialog_widget_private AudioDeviceDialogWidgetPrivate;


struct _audio_device_dialog_widget 
{
    GtkDialog parent;
    AudioDeviceDialogWidgetPrivate *priv;
    
};

struct _audio_device_dialog_widget_class
{
    GtkDialogClass parent_class;
};

struct audio_device_dialog_widget_private 
{
    GtkWidget *save_button, *cancel_button, *audio_dialog_box;
    
};


G_DEFINE_TYPE_WITH_PRIVATE(AudioDeviceDialogWidget,audio_device_dialog_widget,GTK_TYPE_DIALOG);

enum {
    PROP_0,
    PROP_DEVICE_NAMES
};


static void audio_device_dialog_widget_init(AudioDeviceDialogWidget *widget) {
    widget->priv = G_TYPE_INSTANCE_GET_PRIVATE(widget, AUDIO_DEVICE_DIALOG_WIDGET_TYPE, AudioDeviceDialogWidgetPrivate);
    gtk_widget_init_template(GTK_WIDGET(widget));    
}

void audio_device_item_free(audio_device_item **device_items) {
    gsize i=0;
    while (device_items[i] != NULL) {
        free(device_items[i]->server_name);
        free(device_items[i]->device_name);
        free(device_items[i]->device_desc);
        free(device_items[i]);
        i++;
    }
    free(device_items);
}



AudioDeviceDialogWidget *audio_device_dialog_widget_new(audio_device_item **device_items) {
    return g_object_new(AUDIO_DEVICE_DIALOG_WIDGET_TYPE,"devices_list",device_items,NULL);
}

static void audio_device_dialog_widget_set_devices(AudioDeviceDialogWidget *widget,const audio_device_item **device_items) {
    g_return_if_fail(AUDIO_DEVICE_DIALOG_WIDGET(widget));
    int i=0;
    GVariant *gsettings_audio_in_variant=tux_gsettings_audio_devices_in_load();
    while (device_items[i] != NULL) {
        char *server_name = device_items[i]->server_name;
        char *device_name = device_items[i]->device_name;
        char *device_desc = device_items[i]->device_desc;
        const gchar *audio_in_server_name, *audio_in_device_name;
        gboolean is_available = device_items[i]->is_available;
        
        g_debug("server name %s device name %s device desc %s device boolean %d",server_name,device_name,device_desc,is_available);
        
        gboolean is_button_active = FALSE;
        GVariantIter *iter = g_variant_iter_new(gsettings_audio_in_variant);
       while (g_variant_iter_next(iter,"(&s&s)",&audio_in_server_name,&audio_in_device_name)) {
           if (strcmp(server_name,audio_in_server_name) == 0 && strcmp(device_name,audio_in_device_name) == 0) {
               is_button_active=TRUE;
           }
       }
       g_variant_iter_free(iter);        
                 
        AudioDevBoxWidget *audio_dev_box_widget = audio_dev_box_widget_new(server_name,device_name, device_desc, is_available,is_button_active);
        gtk_box_pack_start(GTK_BOX(widget->priv->audio_dialog_box),GTK_WIDGET(audio_dev_box_widget),FALSE, FALSE, 0);        
        i++;
    }
    g_variant_unref(gsettings_audio_in_variant);
}

static void parse_audio_list(GtkWidget *widget, gpointer data) {
    GList *children = gtk_container_get_children(GTK_CONTAINER(widget));
    GtkLabel *server_name_label = GTK_LABEL(children->data);
    GtkLabel *device_name_label = GTK_LABEL(children->next->data);
    GtkCheckButton *is_enabled_button = GTK_CHECK_BUTTON(children->next->next->next->next->data);
    gboolean is_button_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(is_enabled_button));
    if (is_button_active) {
        tux_gsettings_audio_devices_in_add(gtk_label_get_label(server_name_label),gtk_label_get_label(device_name_label));
    }
    g_list_free(children);
}

static void audio_device_dialog_widget_save_devices(AudioDeviceDialogWidget *widget) {
        g_return_if_fail (AUDIO_DEVICE_DIALOG_WIDGET (widget));
        tux_gsettings_audio_devices_in_new();
        GList *children = gtk_container_get_children(GTK_CONTAINER(widget->priv->audio_dialog_box));
        g_list_foreach(children,(GFunc)parse_audio_list,NULL);       
       g_list_free(children);
       tux_gsettings_audio_devices_in_save();
}




static const audio_device_item **audio_device_dialog_widget_get_devices(AudioDeviceDialogWidget *widget) {
        g_return_val_if_fail (AUDIO_DEVICE_DIALOG_WIDGET (widget), NULL);
       const audio_device_item **devices = NULL;     
        return devices;
}



static void
audio_device_dialog_widget_set_property (GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
{
  AudioDeviceDialogWidget *widget = AUDIO_DEVICE_DIALOG_WIDGET (object);

  switch (prop_id)
    {
    case PROP_DEVICE_NAMES:
        audio_device_dialog_widget_set_devices(widget, g_value_get_pointer(value));
          break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
audio_device_dialog_widget_get_property (GObject         *object,
                        guint            prop_id,
                        GValue          *value,
                        GParamSpec      *pspec)
{
  AudioDeviceDialogWidget *widget = AUDIO_DEVICE_DIALOG_WIDGET (object);

  switch (prop_id)
    {
    case PROP_DEVICE_NAMES:
      g_value_set_pointer (value, audio_device_dialog_widget_get_devices(widget));      
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void audio_device_dialog_cancel_clicked(AudioDeviceDialogWidget *widget, GtkButton *button) {
    g_print("cancel clicked\n");
}

static void audio_device_dialog_save_clicked(AudioDeviceDialogWidget *widget, GtkButton *button) {
    audio_device_dialog_widget_save_devices(widget);
}

static void audio_device_dialog_widget_class_init(AudioDeviceDialogWidgetClass *class) {
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
    GObjectClass *gobject_class = G_OBJECT_CLASS(class);
    
    gobject_class->set_property=audio_device_dialog_widget_set_property;
    gobject_class->get_property=audio_device_dialog_widget_get_property;
    GParamSpec *obj_properties = 
            g_param_spec_pointer("devices_list","Devices List","Devices List",G_PARAM_READWRITE);

    
    g_object_class_install_property(gobject_class, PROP_DEVICE_NAMES,obj_properties);
    
  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/com/tuxin/tuxmusicstudio/audio_device_dialog.glade");
  
 gtk_widget_class_bind_template_child_private(widget_class, AudioDeviceDialogWidget, save_button);
 gtk_widget_class_bind_template_child_private(widget_class, AudioDeviceDialogWidget, cancel_button);
 gtk_widget_class_bind_template_child_private(widget_class, AudioDeviceDialogWidget, audio_dialog_box);
 
 gtk_widget_class_bind_template_callback(widget_class, audio_device_dialog_save_clicked);
 gtk_widget_class_bind_template_callback(widget_class, audio_device_dialog_cancel_clicked);
 
}

